import java.util.Comparator;
import java.util.PriorityQueue;

class Comparator1 implements Comparator<Integer>{
    @Override
    public int compare(Integer o1, Integer o2) {
        return (o1 > o2) ? -1 : ((o1 == 02) ? 0 : 1);
    }

}
public class Main{
    public static void main(String[] args) {
        //topK问题,最小的K个元素
        //

        int[] arr={1,3,18,5,8,2,9};
        int[] ret=smallestK(arr,3);

    }
    public static int[] smallestK(int[] arr, int k) {
        PriorityQueue<Integer> priorityQueue=new PriorityQueue<>();
        for (int i=0;i<arr.length;i++){
            priorityQueue.add(arr[i]);
        }
        int[] ret=new int[k];
        for (int i=0;i<ret.length;i++){
            ret[i]=priorityQueue.poll();
        }
        return ret;
    }
    public static int[] bigtK(int[] arr, int k) {
        int[] ret=new int[k];
        if (arr==null||k<0){
            return ret;
        }
        Comparator1 cm=new Comparator1();
        PriorityQueue<Integer> priorityQueue=new PriorityQueue<>(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2.compareTo(o1);
            }
        });
        for (int i=0;i<k;i++){
            priorityQueue.add(arr[i]);
        }
        for (int i=k;i<arr.length;i++){
            if (arr[i]<priorityQueue.peek()){
                priorityQueue.poll();
                priorityQueue.add(arr[i]);
            }
        }
        for (int i=0;i<ret.length;i++){
            ret[i]=priorityQueue.poll();
        }
        return ret;
    }

}
